// 用户相关的api
const express = require("express");
const router = express.Router();
// 引入passport 检证token
const passport = require("passport");
// 引入Blog
const Blog = require("../models/Blog");

/* $route POST api/blog/publish
 * @desc 返回json
 * @access private
 * 后台发布blog
 */
router.post(
	"/publish",
	passport.authenticate("jwt", { session: false }),
	(req, res) => {
		const blogdata = {
			name: req.body.name,
			tags: req.body.tags,
			image: req.body.image,
			content: req.body.content,
		};

		new Blog(blogdata).save().then((blog) => {
			if (!blog) {
				throw err;
			} else {
				res.json(blog);
			}
		});
	}
);

/* $route POST api/blog/list
 * @desc 返回json
 * @access private
 * 后台获取blog
 */
router.post(
	"/list",
	passport.authenticate("jwt", { session: false }),
	async (req, res) => {
		const limit = req.body.limit;
		const skip = req.body.skip;
		const key = req.body.key;
		const total = await Blog.find();

		Blog.find({
			$or: [
				{ name: { $regex: key, $options: "$i" } },
				{ tags: { $regex: key, $options: "$i" } },
				{ content: { $regex: key, $options: "$i" } },
			],
		})
			.limit(limit)
			.skip(skip)
			.then((blog) => {
				if (!blog) {
					return res.status(404).json("暂无数据！");
				} else {
					res.json({ blog, total: total.length });
				}
			});
	}
);

/* $route POST api/blog/data
 * @desc 返回json
 * @access public
 * 获取blog列表
 */
router.post("/data", (req, res) => {
	const limit = req.body.limit;
	const skip = req.body.skip;
	const key = req.body.key;
	Blog.find({
		$or: [
			{ name: { $regex: key, $options: "$i" } },
			{ tags: { $regex: key, $options: "$i" } },
			{ content: { $regex: key, $options: "$i" } },
		],
	})
		.limit(limit)
		.skip(skip)
		.then((blog) => {
			if (!blog) {
				return res.status(404).json("暂无数据！");
			} else {
				res.json(blog);
			}
		});
});

/* $route POST api/blog/detail
 * @desc 返回json
 * @access public
 * 获取blog详情
 */
router.post("/detail", (req, res) => {
	const _id = req.body._id;
	Blog.findById({ _id }).then((blog) => {
		if (!blog) {
			return res.status(404).json("暂无数据！");
		} else {
			res.json(blog);
		}
	});
});

/* $route POST api/blog/beforeupdate
 * @desc 返回json
 * @access private
 * 后台获取blog详情
 */
router.post(
	"/beforeupdate",
	passport.authenticate("jwt", { session: false }),
	(req, res) => {
		const _id = req.body._id;
		Blog.findById({ _id }).then((blog) => {
			if (!blog) {
				return res.status(404).json("暂无数据！");
			} else {
				res.json(blog);
			}
		});
	}
);

/* $route POST api/blog/update
 * @desc 返回json
 * @access private
 * 后台更新blog
 */
router.post(
	"/update",
	passport.authenticate("jwt", { session: false }),
	(req, res) => {
		const _id = req.body._id;
		const updatedata = {};

		if (req.body.name) updatedata.name = req.body.name;
		if (req.body.tags) updatedata.tags = req.body.tags;
		if (req.body.image) updatedata.image = req.body.image;
		if (req.body.content) updatedata.content = req.body.content;

		Blog.findOneAndUpdate(
			{ _id },
			{ $set: updatedata },
			{ new: true }
		).then((blog) => {
			if (!blog) {
				throw err;
			} else {
				res.json(blog);
			}
		});
	}
);

/* $route POST api/blog/delete
 * @desc 返回json
 * @access private
 * 后台删除blog
 */
router.post(
	"/delete",
	passport.authenticate("jwt", { session: false }),
	(req, res) => {
		const _id = req.body._id;
		Blog.findByIdAndDelete({ _id }).then((blog) => {
			if (!blog) {
				throw err;
			} else {
				res.json(blog);
			}
		});
	}
);

/* $route POST api/blog/love
 * @desc 返回json
 * @access public
 * 点赞blog
 */
router.post("/love", (req, res) => {
	const _id = req.body._id;
	Blog.findByIdAndUpdate({ _id }, { $inc: { love: 1 } }, { new: true }).then(
		(blog) => {
			if (!blog) {
				return res.status(404).json("点赞失败！");
			} else {
				res.json(blog);
			}
		}
	);
});

/* $route POST api/blog/number
 * @desc 返回json
 * @access public
 * 浏览blog
 */
router.post("/number", (req, res) => {
	const _id = req.body._id;
	Blog.findByIdAndUpdate(
		{ _id },
		{ $inc: { number: 1 } },
		{ new: true }
	).then((blog) => {
		if (!blog) {
			return res.status(404).json("请求失败！");
		} else {
			res.json(blog);
		}
	});
});

module.exports = router;
